﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;

namespace MDS
{
    public class ShenzhenExtInfoProvider
    {
        protected ShenzhenExtInfoProviderConfig _config;
        protected System.Timers.Timer _checkDBFTimer;
        protected DateTime? _DBFLastWriteTime;
        protected object _lockObj = new object();
        protected bool _isReading = false;
        protected Dictionary<string, SJSXXNDBFRecord> _recordDic = new Dictionary<string, SJSXXNDBFRecord>();

        public ShenzhenExtInfoProvider(ShenzhenExtInfoProviderConfig config)
        {
            Contract.Requires(config != null);
            this._config = config;
            this._checkDBFTimer = new System.Timers.Timer()
            {
                Interval = this._config.CheckDBFIntervalMS,
                AutoReset = false,
                Enabled = false
            };
            this._checkDBFTimer.Elapsed += new System.Timers.ElapsedEventHandler(_checkDBFTimer_Elapsed);
        }

        void _checkDBFTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                read(this._config.DBFFileFullPath);
            }
            catch (Exception ex)
            {
                Log.Debug(ex);
            }
        }

        public void Start()
        {
            this._checkDBFTimer.Start();
        }
        public void Stop()
        {
            this._checkDBFTimer.Stop();
        }

        public string GetStkIndustryType(string stkId)
        {
            if (this._recordDic.ContainsKey(stkId))
                return this._recordDic[stkId].StkIndustryType;
            else
                return null;
        }

        public long GetTotalCirculatingShare(string stkId)
        {
            if (this._recordDic.ContainsKey(stkId))
                return this._recordDic[stkId].TotalCirculatingShare;
            else
                return 0;
        }

        public SuspendedFlag GetSuspendedFlag(string stkId)
        {
            if (this._recordDic.ContainsKey(stkId)
                && this._recordDic[stkId].ShortPrefix.FirstOrDefault() == 'Y')
            {
                return SuspendedFlag.True;
            }
            else
                return SuspendedFlag.False;

        }

        public long GetTotalIssueQty(string stkId)
        {
            if (this._recordDic.ContainsKey(stkId))
                return this._recordDic[stkId].TotalIssueQty;
            else
                return 0;
        }

        public double GetMaxOrderPrice(string stkId)
        {
            if (this._recordDic.ContainsKey(stkId))
                return this._recordDic[stkId].MaxOrderPrice;
            else
                return 0;
        }

        public double GetMinOrderPrice(string stkId)
        {
            if (this._recordDic.ContainsKey(stkId))
                return this._recordDic[stkId].MinOrderPrice;
            else
                return 0;
        }

        public TradeStatus GetTradeStatus(string stkId)
        {
            if (this._recordDic.ContainsKey(stkId)
               && (this._recordDic[stkId].TradeStatus == "Y" || this._recordDic[stkId].TradeStatus == "Z"))
                return TradeStatus.Y;
            else
                return TradeStatus.N;
        }


        protected void read(string dbfFile)
        {
            if (this._isReading)
                return;
            lock (this._lockObj)
            {
                this._isReading = true;
                List<SJSXXNDBFRecord> records = DBFHelper.ReadRecords<SJSXXNDBFRecord>(dbfFile);
                if (records != null)
                {
                    foreach (var record in records)
                    {
                        this._recordDic[record.StkId] = record;
                    }
                }
                this._isReading = false;
            }

        }
    }
}
